package com.zhuangxiaoyan.spring.srcode.transaction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * @Classname TransactionTest
 * @Description 使用TransactionTemplate 进行编程式事务管理的示例代码如下：
 * @Date 2022/5/1 21:47
 * @Created by xjl
 */
public class Transaction {

    @Autowired
    private TransactionTemplate transactionTemplate;

    public void testTransaction() {

        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {

                try {

                    // ....  业务代码
                } catch (Exception e){
                    //回滚
                    transactionStatus.setRollbackOnly();
                }

            }
        });
    }
}
